#include "mcmcMap.h"


////////////////////////////////////////////////
// class for exchanging data

// constructor and destructor
MCMC_SegSetting::MCMC_SegSetting()
{
	btexture	= false;
	bsave_seg	= true;
	dscale		= 3.0;
	nstep_from	= 0;
	nstep_to	= 6000;
	bsame_size	= true;
	bload_data	= false;
	pfile_name	= NULL;
}

MCMC_SegSetting::~MCMC_SegSetting()
{
	if (pfile_name != NULL)
		delete []pfile_name;
	pfile_name = NULL;
}

// operator
MCMC_SegSetting& MCMC_SegSetting::operator=(const MCMC_SegSetting a)
{
	if (this != &a)
	{
		nheight		= a.nheight;
		nwidth		= a.nwidth;
		dscale		= a.dscale;
		bsave_seg	= a.bsave_seg;
		bcolor		= a.bcolor;
		btexture	= a.btexture;
		nheight_seg	= a.nheight_seg;
		nwidth_seg	= a.nwidth_seg;
		bsame_size	= a.bsame_size;
		bload_data	= a.bload_data;

		SetLoadFileName(a.pfile_name);
	}
	return *this;
}

void MCMC_SegSetting::SetLoadFileName(char *fname)
{
	if (pfile_name != NULL)
		delete []pfile_name;
	pfile_name = NULL;
	if (fname != NULL)
	{
		pfile_name = new char[strlen(fname)+1];
		strcpy(pfile_name, fname);
	}
}



////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// construction & destruction
MCMC_Map::MCMC_Map()
{
	// create region layer
	player_region = new MCMC_Layer_Regions();

	// the initial step is 0
	step = 0;
}


MCMC_Map::~MCMC_Map()
{
	if (player_region != NULL)
		delete player_region;
	player_region = NULL;
}


////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// construction & destruction
MCMC_Map_2Layers::MCMC_Map_2Layers()
:MCMC_Map()
{
}

MCMC_Map_2Layers::~MCMC_Map_2Layers()
{
}

// operators
MCMC_Map_2Layers& MCMC_Map_2Layers::operator=(MCMC_Map_2Layers &a)
{
	if (this != &a)
	{
		// region layer
		*player_region	= (*a.player_region);
		ras_region_label= a.ras_region_label;

		prespFilter		= a.prespFilter;
		mx_overlapping	= a.mx_overlapping;
	}
	return *this;
}

void MCMC_Map_2Layers::operator>>(ofstream &strm)
{
	(*player_region) >> strm;
}

void MCMC_Map_2Layers::operator<<(ifstream &strm)
{
	(*player_region) << strm;
}

void MCMC_Map_2Layers::SaveToFile(char *file_name)
{
	ofstream file;

	file.open(file_name, ios::out|ios::binary);
	if (!file.fail())
	{
		(*this) >> file;
		file.close();
	}
}

bool MCMC_Map_2Layers::ReadFromFile(char *file_name)
{
	ifstream file;

	file.open(file_name, ios::in|ios::binary|ios::_Nocreate);
	if (!file.fail())
	{
		(*this) << file;
		file.close();
		return true;
	}
	else
		return false;
}


void MCMC_Map_2Layers::SetFilterResp(MCMC_FilterResp &resp)
{
	prespFilter = &resp;

	ras_region_label.SetDimension(resp.rows(), resp.cols());
}


void MCMC_Map_2Layers::ComputeTotalEnergy(const bool brecompute_energyshape)
{
	// region layer
	LayerRegion()->ComputeTotalEnergy(brecompute_energyshape);
}


// synthesize a color/grey scale image from curve and region layer
void MCMC_Map_2Layers::Reconstruct(void)
{
	/////////////
	// region layer
	// synthesize the region layer
//	LayerRegion()->Reconstruct(prespFilter->ras_region_rec, ras_region_label);
}


void MCMC_Map_2Layers::Reconstruct(Raster_Color<uchar> &ras_syn_image)
{
	/////////////
	// region layer
	ras_syn_image.SetDimension(Height(),Width());
	// synthesize the region layer
	LayerRegion()->Reconstruct(ras_syn_image);
/*
	//////////////////////////
	// combine the two layers
	for (j=0; j<prespFilter->rows(); j++)
		for (i=0; i<prespFilter->cols(); i++)
			{
				temp = prespFilter->ras_region_rec(j,i);

				ras_syn_image.R(j,i) = (uchar)temp;
				ras_syn_image.G(j,i) = ras_syn_image.R(j,i);
				ras_syn_image.B(j,i) = ras_syn_image.R(j,i);
			}
*/
}

// synthesize a color/grey scale image from curve and region layer
void MCMC_Map_2Layers::Synthesize(Raster_Color<uchar> &ras_syn_image, const char flag)
{
//Reconstruct(ras_syn_image);
//return;

	/////////////////////////
	// region layer
	ras_syn_image.SetDimension(prespFilter->rows(), prespFilter->cols());

	if (flag == 0)
	{
		// from the label map

		// synthesize the region layer
		Raster<int>	ras_region_label;
		ras_region_label.SetDimension(prespFilter->rows(), prespFilter->cols());
		LayerRegion()->LabelAtomicRegions(ras_region_label);
		
		LayerRegion()->Synthesize(ras_syn_image, &ras_region_label);
	}
	// from the pixel domain
	else
		LayerRegion()->Synthesize(ras_syn_image);
}
